package cn.sdc.bugKiller.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.sdc.bugKiller.RequestContants;
import cn.sdc.bugKiller.service.ContextService;
import cn.sdc.bugKiller.util.ParamUtil;
import cn.sdc.sfw.util.ClassUtil;

public abstract class BasicServlet extends HttpServlet implements RequestContants{
	
	public final static String STR_METHOD = "method";
	public final static String STR_MENUSTYLE = "menuMap";
	
	protected String methodName;
	protected String beanName = "";
	protected String menu = MENU_NO;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		clearErrorMap();
		setMethodName(req);
		exceute(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	public abstract void exceute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException;
	
	public void forward(HttpServletRequest req, HttpServletResponse resp,String url, HashMap params)
			throws ServletException, IOException {
		if(params != null && !params.isEmpty()){
			for(Map.Entry entry : (Set<Map.Entry>)params.entrySet()){
				req.setAttribute((String) entry.getKey(), entry.getValue());
			}
		}
		req.getRequestDispatcher(url).forward(req, resp);
	}
	
	public void forward(HttpServletRequest req, HttpServletResponse resp,String url)
			throws ServletException, IOException {
		setMenuStyle(req);
		setErrorMap(req);
		setErrorMap(req);
		forward(req,resp,url,null);
	}
	
//	public void forwardWithMethod(HttpServletRequest req, HttpServletResponse resp,String url, String method,HashMap params)
//			throws ServletException, IOException {
//		if(method!=null && !method.isEmpty()){
//			req.setAttribute(STR_METHOD, method);
//		}else{
//			req.removeAttribute(STR_METHOD);
//		}
//		forward(req,resp,url,params);
//	}
//	
//	public void forwardWithMethod(HttpServletRequest req, HttpServletResponse resp,String url, String method)
//			throws ServletException, IOException {
//		req.setAttribute(STR_METHOD, method);
//		forward(req,resp,url);
//	}

	public Object getBean(HttpServletRequest req, Class beanClass,String... excludes){
		Object toBean = ClassUtil.newInstance(beanClass);
		ParamUtil.copyBean(req, beanName, toBean, excludes);
		return toBean;
	}
	public Object getBeanWithFields(HttpServletRequest req, Class beanClass,String... includes){
		Object toBean = ClassUtil.newInstance(beanClass);
		ParamUtil.copyExpectedFiledBean(req,  beanName, toBean, includes);
		return toBean;
	}
	
	public void setMenuStyle(HttpServletRequest req){
		HashMap<String, String> styleMap = new HashMap<String, String>();
		if(!menu.equals(MENU_NO)){
			styleMap.put(menu," Activity");
		}
		req.setAttribute(STR_MENUSTYLE, styleMap);
	}
	
	
	//get set method
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	
	public void setMethodName(HttpServletRequest req){
		if(this.methodName == null){
			this.methodName = req.getParameter(STR_METHOD);
		}
		if(this.methodName==null){
			methodName = (String)req.getAttribute(STR_METHOD);
		}
		if(methodName==null){
			methodName = getInitParameter(STR_METHOD);
		}
	}
	
	private void clearMethodName(){
		this.methodName = "";
	}
	
	private void clearMenuStyle(){
		this.menu = "";
	}
	
	public Integer getParamId(HttpServletRequest req){
		final String strParamId = "id";
		String paramId = req.getParameter(strParamId);
		if(paramId==null){
			paramId = req.getParameter(beanName+"."+strParamId);
		}
		if(paramId==null || paramId.isEmpty()){
			paramId = (String)req.getAttribute(strParamId);
		}
		if(paramId!=null)
			return Integer.parseInt(paramId);
		else{
			return null;	
		}
	}
	
	public Integer getCurrentMemberId(){
		return ContextService.getCurrentUserId();
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public void setMenu(String menu) {
		this.menu = menu;
	}
	
	public void clearErrorMap(){
		filedErrorMap.clear();
		pageErrorMap.clear();
	}
	
	private void setErrorMap(HttpServletRequest req) {
		req.setAttribute(STR_FILED_ERR_MAP, filedErrorMap);
		req.setAttribute(STR_PAGE_ERR_MAP, pageErrorMap);
		
	}
}
